#pragma once

#include <array>
#include <vector>

namespace solution_036
{

class Solution {
public:
    bool isValidSudoku(const std::vector<std::vector<char>>& board)
    {
        std::array<bool, 9> row_check;
        std::array<bool, 9 * 9> col_checks;
        std::array<bool, 9 * 9> box_checks;

        col_checks.fill(false);
        box_checks.fill(false);

        for (int row = 0; row < 9; ++row) {
            row_check.fill(false);
            for (int col = 0; col < 9; ++col) {
                auto ch = board[row][col];
                switch (ch) {
                    case '.': continue;

                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9': {
                        char num = ch - '0' - 1;
                        auto box = (row / 3) * 3 + (col / 3);

                        if (row_check.at(num)) return false;
                        row_check.at(num) = true;

                        if (col_checks.at(col * 9 + num)) return false;
                        col_checks.at(col * 9 + num) = true;

                        if (box_checks.at(box * 9 + num)) return false;
                        box_checks.at(box * 9 + num) = true;
                    } break;

                    default: return false;
                }
            }
        }
        return true;
    }
};

}  /* namespace solution_036 */
